package cn.liziguo.tank.config;

import cn.liziguo.tank.constants.ImageConst;
import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.texture.Texture;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.util.Duration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Liziguo
 * @date 2022/7/11 13:43
 */
public class GameConfig {
    /**
     * 宽
     */
    public static final int WIDTH = 1200;
    /**
     * 高
     */
    public static final int HEIGHT = 960;
    /**
     * 地图元素单位宽高
     */
    public static final int MAP_UNIT = 24;
    /**
     * 坦克的宽高
     */
    public static final int TANK_UNIT = 39;

    /**
     * 玩家最大生命值
     */
    public static final int PLAYER_MAX_HP = 10;
    /**
     * 玩家射击cd
     */
    public static final long PLAYER_SHOOT_CD = 100;
    /**
     * 玩家技能cd
     */
    public static final long PLAYER_SKILL_CD = 200;
    /**
     * 玩家移动速度 像素/每秒
     */
    public static final double PLAYER_SPEED = 200;

    /**
     * 敌人最大生命
     */
    public static final int ENEMY_MAX_HP = 5;
    /**
     * 敌人射击cd
     */
    public static final long ENEMY_SHOOT_CD = 500;
    /**
     * 敌人技能cd
     */
    public static final long ENEMY_SKILL_CD = 2000;
    /**
     * 敌人移动速度 像素/每秒
     */
    public static final double ENEMY_SPEED = 150;

    /**
     * 技能的命中伤害
     */
    public static final int BULLET_PLUS_ATTACK = 6;
    /**
     * 技能的爆炸伤害
     */
    public static final int BULLET_PLUS_BOOM_ATTACK = 6;
    /**
     * 技能的爆炸半径 像素
     */
    public static final int BULLET_PLUS_BOOM_SIZE = 200;

    /**
     * 坦克被击败后掉落道具的概率(1为百分之百)
     */
    public static final double ITEM_DROP_PROBABILITY = 0.1;

    /**
     * 关卡文件加载路径
     */
    public static final String LEVEL_PATH = "assets/levels/level%d.json";
    /**
     * 关卡初始敌人数量
     */
    public static final int LEVEL_INIT_ENEMY_COUNT = 10;
    /**
     * 关卡加载敌人数量
     */
    public static final int LEVEL_LOAD_ENEMY_COUNT = 20;
    /**
     * 关卡加载敌人间隔 单位毫秒
     */
    public static final long LEVEL_ENEMY_LOAD_INTERVAL = 100;

    /**
     * 爱心道具回血量
     */
    public static final int ITEM_HEART = 5;
    /**
     * 无敌道具的持续时间 单位秒
     */
    public static final Duration INVINCIBLE_TIME = Duration.seconds(12);
    /**
     * 冻结敌人的道具持续时间 单位秒
     */
    public static final Duration FREEZE_ENEMY_TIME = Duration.seconds(10);
    /**
     * 家庭围栏加强时间 单位秒
     */
    public static final Duration HOME_FENCE_REINFORCE_TIME = Duration.seconds(15);
    /**
     * 加速道具增加速度 像素/每秒
     */
    public static final int SPEEDING_NUM = 200;

    /**
     * 子弹最低等级
     */
    public static final int BULLET_LEVEL_MIN = 1;
    /**
     * 子弹最高等级
     */
    public static final int BULLET_LEVEL_MAX = 3;

    /**
     * 子弹默认等级
     */
    public static final int BULLET_LEVEL_DEFAULT = BULLET_LEVEL_MIN;

    /**
     * 子弹能摧毁森林的等级
     */
    public static final int DESTROY_GREENS_BULLET_LEVEL = 2;
    /**
     * 子弹能摧毁石头的等级
     */
    public static final int DESTROY_STONE_BULLET_LEVEL = BULLET_LEVEL_MAX;

    /**
     * 家的坐标
     */
    public static final Point2D HOME_POINT;
    /**
     * 家围墙的坐标
     */
    public static final List<Point2D> HOME_FENCE_POINT;
    /**
     * 玩家坐标
     */
    public static final Point2D PLAYER_1_INIT_POINT;
    public static final Point2D PLAYER_2_INIT_POINT;

    static {
        final int width = WIDTH / 2;
        final int height = HEIGHT;

        HOME_POINT = new Point2D(width - MAP_UNIT, height - MAP_UNIT * 2);

        List<Point2D> list = new ArrayList<>(8);

        list.add(new Point2D(width - MAP_UNIT * 2, height - MAP_UNIT));
        list.add(new Point2D(width - MAP_UNIT * 2, height - MAP_UNIT * 2));
        list.add(new Point2D(width - MAP_UNIT * 2, height - MAP_UNIT * 3));
        list.add(new Point2D(width - MAP_UNIT, height - MAP_UNIT * 3));
        list.add(new Point2D(width, height - MAP_UNIT * 3));
        list.add(new Point2D(width + MAP_UNIT, height - MAP_UNIT * 3));
        list.add(new Point2D(width + MAP_UNIT, height - MAP_UNIT * 2));
        list.add(new Point2D(width + MAP_UNIT, height - MAP_UNIT));

        HOME_FENCE_POINT = List.copyOf(list);

        // 设置玩家出生位置为 家的左边
        PLAYER_1_INIT_POINT = new Point2D(WIDTH / 2.0 - GameConfig.MAP_UNIT * 4, HEIGHT - TANK_UNIT - 1);
        PLAYER_2_INIT_POINT = new Point2D(WIDTH / 2.0 + GameConfig.MAP_UNIT * 4 - TANK_UNIT, HEIGHT - TANK_UNIT - 1);
    }

    /**
     * 根据子弹等级获得子弹样式
     *
     * @param level 子弹等级
     * @return 样式
     */
    public static Node getBulletImageByLevel(int level) {
        Texture image = FXGL.texture(ImageConst.BULLET_NORMAL);
        switch (level) {
            case 2 -> image = image.multiplyColor(Color.GOLDENROD);
            case 3 -> image = image.multiplyColor(FXGLMath.randomColor());
        }
        return image;
    }
}
